<?php

namespace App\Http\Controllers\admin\defaults;


use App\Helpers\commonFuc;
use App\Models\Tool\MakeAddHtmlAndJs;
use App\Models\Tool\MakeControllerT;
use App\Models\Tool\MakeInfoHtmlAndJs;
use App\Models\Tool\MakeListHtmlAndJs;
use App\Models\Tool\MakeRoutes;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class ToolController extends BaseController
{
    private $tpviewhead='admin.defaults.tool.';
    private $table='admin';

    function __construct(Request $thisRequest)
    {
        parent::__construct($thisRequest,$this->tpviewhead,$this->table);
    }

    function index(){

        $phpdata = [
            'tables' => $this->TableFieldCache(),
            'table_prefix' =>env('DB_PREFIX'),
        ];
        if($this->isPostOrAjax){
            return commonFuc::data_reture_json($phpdata, 200);
        }
        return view($this->tpviewhead . 'tool',$phpdata);
    }

    /**删除生成的类文件
     * @return \App\Helpers\Json|\App\Helpers\unknown|array|string
     */
    function dele(){
        $inputdata=($this->thisRequest->all());

        $xxxController =$inputdata['controller'];
        $conhead =$inputdata['table'];
        $commonpathname='addon';
        $controller_path= ('Http/Controllers/admin/'.$commonpathname.'/');
        $views_path= ('views/admin/'.$commonpathname.'/'.$conhead.'/');
        $js_path= ('style_js_admin/viewjs/'.$commonpathname.'/'.$conhead.'/');
        commonFuc::deleFileOrDir(public_path($js_path));
        commonFuc::deleFileOrDir(resource_path($views_path));
        commonFuc::deleFileOrDir(app_path($controller_path.$xxxController.'.php'));
        return commonFuc::data_reture_json([], 200, '删除成功');
    }

    /**
     * 生成类文件
     */
    function make(){
        $inputdata=($this->thisRequest->all());

        $conname =$inputdata['controllername'];

        $roures = [];

        $data = [
            'add'=>[
                "js" => "",
                "html" => "",
            ],'edit'=>[
                "js" => "",
                "html" => "",
            ],'lists'=>[
                "js" => "",
                "html" => "",
            ],'Controllers'=>[
                "js" => "",
                "html" => "",
            ]
        ];

        $xxxController =$inputdata['controller'];
        $conhead =$inputdata['table'];
        $commonpathname='addon';
        $controller_path= ('Http/Controllers/admin/'.$commonpathname.'/');
        $views_path= ('views/admin/'.$commonpathname.'/'.$conhead.'/');
        $js_path= ('style_js_admin/viewjs/'.$commonpathname.'/'.$conhead.'/');
        $base_path = base_path() . "/";
// 创建一个目录
        $path=resource_path($views_path);

        if (!is_dir($path))mkdir($path,0755,true);
        $path=app_path($controller_path);
        if (!is_dir($path))mkdir($path,0755,true);
        $path=public_path($js_path);
        if (!is_dir($path))mkdir($path,0755,true);


        $name = 'add';
        if(isset($inputdata[$name]) && $inputdata[$name]['status']==1){
            $html_filename=$name.'.blade.php';
            $html_file = $views_path.$html_filename;
            $js_filename=$name.'.blade.js';
            $js_file = $js_path.$js_filename;
            MakeAddHtmlAndJs::make($html_file,$html_filename,$js_file,$js_filename,$inputdata[$name]['list']);
            $roures[] = $name;
            $data[$name]['js'] = $base_path.'public/'.$js_file;
            $data[$name]['html'] = $base_path.'resources/'.$html_file;
        }

        $name = 'edit';
        if(isset($inputdata[$name]) && $inputdata[$name]['status']==1){
            $html_filename=$name.'.blade.php';
            $html_file = $views_path.$html_filename;
            $js_filename=$name.'.blade.js';
            $js_file = $js_path.$js_filename;
            MakeAddHtmlAndJs::make($html_file,$html_filename,$js_file,$js_filename,$inputdata[$name]['list']);
            $roures[] = $name;
            $data[$name]['js'] = $base_path.'public/'.$js_file;
            $data[$name]['html'] = $base_path.'resources/'.$html_file;
        }

        $name = 'lists';
        if(isset($inputdata[$name]) && $inputdata[$name]['status']==1){
            $html_filename=$name.'.blade.php';
            $html_file = $views_path.$html_filename;
            $js_filename=$name.'.blade.js';
            $js_file = $js_path.$js_filename;
            MakeListHtmlAndJs::make($html_file,$html_filename,$js_file,$js_filename,$inputdata[$name]['list'],$inputdata);
            $roures[] = $name;
            $data[$name]['js'] = $base_path.'public/'.$js_file;
            $data[$name]['html'] = $base_path.'resources/'.$html_file;


            if(in_array('info',$inputdata['buttons_row'])){
                $name = 'info';
                $html_filename=$name.'.blade.php';
                $html_file = $views_path.$html_filename;
                $js_filename=$name.'.blade.js';
                $js_file = $js_path.$js_filename;
                MakeInfoHtmlAndJs::make($html_file,$html_filename,$js_file,$js_filename);
                $roures[] = $name;
                $data[$name]['js'] = $base_path.'public/'.$js_file;
                $data[$name]['html'] = $base_path.'resources/'.$html_file;
            }


        }
        $name = 'dele';
        if(isset($inputdata[$name]) && $inputdata[$name]['status']==1){
            $roures[] = "dele";
        }
        $roures[] = "save";
        $roures[] = "info";
        //生成控制器
        $html_file = $controller_path.$xxxController.'.php';
        MakeControllerT::make($html_file,$xxxController,$controller_path,$views_path,$inputdata);
        $data['Controllers']['html'] = $base_path.'app/'.$html_file;
        //生成路由
        if($inputdata['table']!='zdymydqltable'){
            MakeRoutes::make($roures,$controller_path,$xxxController,$conname);
        }

        return commonFuc::data_reture_json($data,200,'');

    }



    function lookfile(){
        // 获取文件内容，不存在会抛出 FileNotFoundException 异常
        $inputdata=($this->thisRequest->all());
        $path =  $inputdata['path'];
        $path = str_replace("\\", "/",$path);
        //$phpdata=File::getRequire($path);
       /* $phpdata=File::get($path, $lock = false);
        return view($this->tpviewhead . 'toollookfile',['data'=>$phpdata]);*/

        $filename = $path;
        $fp = fopen($filename, "r");//open file in read mode

        $contents = fread($fp, filesize($filename));//read file

        //echo $contents;//printing data of file
        fclose($fp);//close file
        return view($this->tpviewhead . 'toollookfile',['data'=>$contents]);
//更多请阅读：https://www.yiibai.com/php/php-read-file.html


        //return $content;
    }

};
